/*  File Name	: Poker.cpp
 *  Author		: Javie Pong
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-8-5
 *  Description:
 */

#include <algorithm>
#include "Poker.h"
#include "EngineMonitor.h"
#include "Logger.h"
#include "Table.h"

Card::Card(int p)
{
	this->reset();
}

Card::~Card()
{
}

void Card::reset(void)
{
	this->number_ = CARD_NONE;
	this->type_ = CARDTYPE_NONE;
	is_public_card_ = false;
	is_used_ = false;
	name_ = "";
}

void Card::set_info(CARDTYPE type,CARDNUMBER num,std::string name)
{
	this->number_ = num;
	this->type_ = type;
	this->name_ = name;

}

//////////////////////////////////////////////////////////////////////////////
Poker::Poker(int p)
{
	this->reset();
}

Poker::~Poker()
{
	for(int i =0;i<(int)this->card_vec_.size();i++)
	{
		Card *card= this->card_vec_[i];
		if(card!=NULL)
		{
			this->table_->monitor()->card_pool().push(card);
		}
	}
	this->card_vec_.clear();
}

CardVec &Poker::card_vec(void)
{
	return this->card_vec_;
}

int Poker::card_index(int card_num)
{
	return ((card_num-1)%13);
}

void Poker::reset(void)
{
	this->table_ = NULL;
	//this->shuffle_poker();
}

int Poker::init(Table *table)
{
	this->table_ = table;
	if(NULL==this->table_)
	{
		return -1;
	}

	this->card_vec_.clear();

	int index = 0;
	for(int i=(int)CARDTYPE_DIAMOND;i<=(int)CARDTYPE_SPADE;i++)
	{
#ifndef ONE_MAX
		for(int j=(int)CARD_ONE;j<=(int)MAX_CARD;j++)
#else
		index =1;
		for(int j=(int)CARD_TWO;j<=(int)MAX_CARD;j++)
#endif
		{
			Card *card= NULL;
			if(this->table_->monitor()->card_pool().pop(card)<0)
			{
				LOG((ERROR_LOG,"Poker::init error"));
				return -1;
			}
			card->set_info((CARDTYPE)i,(CARDNUMBER)j,CardName[i-1][index]);
			this->card_vec_.push_back(card);
			if((++index)>=13)
			{
				index = 0;
			}
		}
	}
	return 0;
}

int Poker::shuffle_poker(void)
{
	std::random_shuffle(this->card_vec_.begin(),this->card_vec_.end());
	return 0;
}

